<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/5
 * Time: 18:48
 */

namespace Admin\Controller;


use Admin\Common\CommonController;

/**
 * Class GroupController
 * @package Admin\Controller
 * 权限组管理
 */

class GroupController extends CommonController
{
    //权限组列表
    public function lists(){
       //找出所有的权限组
        $groups=M('authGroup')->select();
        foreach($groups as $k=>$v){
            $groups[$k]['rulename']=M('authRule')->where("id","IN",$groups['rules'])->getField('title',true);
        }
        $this->assign('groups',$groups);
//        dd($groups);
        $this->display();
    }
  //权限组添加
    public function addgroup(){
        $id=I('get.id');
        if($id){
            //查找出旧数据
            $olddate=M('authGroup')->where("id=$id")->find();
            $olddate['rules']=explode(',',$olddate['rules']);
            $this->assign('olddate',$olddate);
            $rule=M('authRule')->getField('id',true);
            $this->assign('rule',$rule);
        }
            //找出所有的权限规则
            $rules=M('authRule')->select();
        $this->assign('rules',$rules);
        if(IS_POST){
            if($id){
             //表示编辑
                $_POST['id']=$id;
            }
            $_POST['rules']=implode(',',I('post.rules'));
            $res=D('authGroup')->addgroup(I('post.'));
//            dd($res);die;
            $this->jump($res,'lists');
        }
//        dd($rules);
//        dd($olddate);
        $this->display();
    }
    //权限组删除
    public  function delgroup(){
        $id=I('get.id');
        $res=M('authGroup')->where("id=$id")->delete();
        if($res){
          $this->success('删除成功');die;
        }else{
            $this->error('删除失败');die;
        }
    }
}